package com.juku2024.juku.fragment

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.juku2024.juku.R
import com.juku2024.juku.adapter.HomeTabFragmentAdapter
import com.juku2024.juku.base.BaseFragment
import com.juku2024.juku.model.RankingResponse
import com.juku2024.juku.viewmodel.UnlockViewModel
import com.juku2024.juku.widget.LoadingView

class RankingFragment : BaseFragment() {
    
    private lateinit var tabLayout: TabLayout
    private lateinit var viewPager: ViewPager2
    private lateinit var loadingView: LoadingView
    private val fragments = ArrayList<BaseFragment>()
    private val viewModel by lazy { getViewModel(requireActivity().viewModelStore, UnlockViewModel::class.java) }

    override fun layoutView(inflater: LayoutInflater, container: ViewGroup?): View {
        return inflater.inflate(R.layout.fragment_ranking, container, false)
    }

    override fun initView(rootView: View) {
        super.initView(rootView)
        tabLayout = rootView.findViewById(R.id.rank_tabLayout)
        viewPager = rootView.findViewById(R.id.rank_viewPager)
        loadingView = rootView.findViewById(R.id.rank_loadingView)

        viewPager.orientation = ViewPager2.ORIENTATION_HORIZONTAL
        viewPager.isUserInputEnabled = true
    }

    override fun onResume() {
        super.onResume()
        if (fragments.isEmpty()) {
            loadingView.showLoading()
            doHttpRanking()
        }
    }

    override fun bindEvent() {
        super.bindEvent()
        tabLayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) {}
            override fun onTabUnselected(tab: TabLayout.Tab?) {
                val tvTab = tab?.customView?.findViewById<TextView>(R.id.tvHomeTab) ?: return
                tvTab.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)//加粗
                tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13f)
            }

            override fun onTabSelected(tab: TabLayout.Tab?) {
                val tvTab = tab?.customView?.findViewById<TextView>(R.id.tvHomeTab) ?: return
                tvTab.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                tvTab.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15f)
            }
        })
    }

    private fun doHttpRanking() {
        viewModel.getRankingNavList( {
            if (isFragmentNotActive()) return@getRankingNavList
            loadingView.hideLoading()
            if (it.data == null || it.data.navs.isNullOrEmpty()) {
                loadingView.showEmptyStatus("当前榜单无内容！")
                return@getRankingNavList
            }
            initTab(it)
        }, {
            if (isFragmentNotActive()) return@getRankingNavList
            loadingView.hideLoading()
            loadingView.showErrorStatus(it, getString(R.string.retry),) {
                loadingView.showLoading()
                loadingView.postDelayed({
                    doHttpRanking()
                }, 500)
            }
        })
    }


    private fun initTab(model: RankingResponse) {
        model.data.navs.forEachIndexed { index, rankNavModel ->
            val rankingListFragment = RankingListFragment()
            val bundle = Bundle()
            if (index == 0 && !model.data.videos.isNullOrEmpty()) {
                bundle.putParcelableArrayList("ranking_data", model.data.videos)
            }
            bundle.putInt("ranking_id", rankNavModel.id)
            rankingListFragment.arguments = bundle
            fragments.add(rankingListFragment)
        }
        val tabAdapter = HomeTabFragmentAdapter(this, fragments)
        viewPager.adapter = tabAdapter
        TabLayoutMediator(tabLayout, viewPager, true) { tab, position ->
            tab.setCustomView(R.layout.layout_home_tab)
            val tvTab = tab.customView?.findViewById<TextView>(R.id.tvHomeTab)
            tvTab?.text = model.data.navs[position].name
        }.attach()
    }

}